package com.woniuxy.mesboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.woniuxy.mesboot.dto.TicketsDetailsDTO;
import com.woniuxy.mesboot.dto.WorkTicketsDTO;
import com.woniuxy.mesboot.entity.MesWorktickets;
import com.woniuxy.mesboot.mapper.MesTicketdetailMapper;
import com.woniuxy.mesboot.mapper.MesWorkticketsMapper;
import com.woniuxy.mesboot.service.WorkTicketsService;
import com.woniuxy.mesboot.vo.TicketDetailsVO;
import com.woniuxy.mesboot.vo.WorkTicketVO;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 黑川
 * @version : 1.0
 * @Project : mes-boot
 * @Package : com.woniuxy.mesboot.service.impl
 * @ClassName : .java
 * @createTime : 2023/5/24 9:42
 * @Description :
 */
@Service
public class WorkTicketsServiceImpl implements WorkTicketsService {
    @Resource
    private MesWorkticketsMapper workticketsMapper;
    @Resource
    private MesTicketdetailMapper ticketdetailMapper;

    @Override
    public int selectIdByNo(String ticketNo) {
        return workticketsMapper.selectIdByNo(ticketNo);
    }

    @Override
    public List<TicketDetailsVO> selectAllDetails(Integer id) {
        return workticketsMapper.selectAllDetails(id);
    }

    @Override
    public List<WorkTicketVO> selectAll(WorkTicketsDTO tickets) {
        return workticketsMapper.selectAll(tickets);
    }

    @Override
    public int deleteByPrimaryKey(Integer ticketId) {
        int i = workticketsMapper.deleteByPrimary(ticketId);
        if (i>0){
            ticketdetailMapper.deleteByTId(ticketId);
        }
        return i;
    }

    @Override
    public int insert(MesWorktickets record) {
        return workticketsMapper.insert(record);
    }

    @Override
    public int insertSelective(TicketsDetailsDTO detailsDTO) {
        String ticketNo = DateUtil.format(new Date(), "yyyyMMddHH")+ RandomUtil.randomNumbers(4);
        detailsDTO.setTicketNo(ticketNo);
        return workticketsMapper.insertSelective(detailsDTO);
    }

    @Override
    public MesWorktickets selectByPrimaryKey(Long id) {
        return workticketsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(MesWorktickets record) {
        return workticketsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MesWorktickets record) {
        return workticketsMapper.updateByPrimaryKey(record);
    }
}
